package com.goldcn.sys.controller;


import com.goldcn.common.CommonUtils;
import com.goldcn.common.ResultData;
import com.goldcn.sys.model.SysApp;
import com.goldcn.sys.model.SysConfigExt;
import com.goldcn.sys.model.SysPayment;
import com.goldcn.sys.service.EmployeeService;
import com.goldcn.sys.service.SysAppService;
import com.goldcn.sys.service.SysConfigService;
import com.goldcn.sys.service.SysPaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author Li
 * mail:
 * Date: 2017/6/21
 * Time: 14:36
 * To change this template use File | Settings | File Templates.
 */
@RestController
@RequestMapping(value = "/sysConfig")
@Api(value = "系统参数设置",description = "开账设置")
public class SysConfigController {

    @Autowired
    SysConfigService sysConfigService;

    @Autowired
    SysPaymentService sysPaymentService;

    @Autowired
    EmployeeService employeeService;

    @Autowired
    SysAppService sysAppService;

    @ApiOperation(value="公司主键查询账户设置")
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="cID",dataType ="long",required=true,value="公司主键",defaultValue="796235203885682688")})
    @RequestMapping(value = "/id", method = RequestMethod.GET)
    public Object getEmployee(@RequestParam Long cID) {
        return sysConfigService.viewInfo(cID);
    }

    @ApiOperation(value ="添加账户设置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="form",name="code",dataType ="String",required=true,value="key字母下划线数字",defaultValue=""),
            @ApiImplicitParam(paramType="form",name="value",dataType ="long",required=true,value="key对应得值",defaultValue=""),
            @ApiImplicitParam(paramType="form",name="remark",dataType ="String",required=false,value="备注",defaultValue=""),
    })
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(@RequestBody com.goldcn.common.RequestParam  params){
        Map<String,String> param = (Map<String,String>)params.getParams();
        return sysConfigService.addOrUpdate(param);
    }

    @ApiOperation(value ="修改账户设置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="form",name="code",dataType ="String",required=true,value="key字母下划线数字",defaultValue=""),
            @ApiImplicitParam(paramType="form",name="value",dataType ="long",required=true,value="key对应得值",defaultValue=""),
            @ApiImplicitParam(paramType="form",name="remark",dataType ="String",required=false,value="备注",defaultValue=""),
    })
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object update(@RequestBody com.goldcn.common.RequestParam  params){
        return add(params);
    }

    @RequestMapping(value = "/updateStartupEnabled",method = RequestMethod.POST)
    public Object updateStartupEnabled(@RequestBody SysConfigExt ext){
        return sysConfigService.updateEnabled(ext,1);
    }

    @RequestMapping(value = "/updateAppEnabled",method = RequestMethod.POST)
    public Object updateAppEnabled(@RequestBody SysConfigExt ext){
        return sysConfigService.updateEnabled(ext,2);
    }


    @RequestMapping(value = "/updateExt",method = RequestMethod.POST)
    public Object updateExt(@RequestBody  SysConfigExt sysConfigExt){
        return sysConfigService.updateExt(sysConfigExt);
    }

    @RequestMapping(value = "/updateMsgTemp",method = RequestMethod.POST)
    public Object updateMsgTemp(@RequestBody com.goldcn.common.RequestParam  params){
        Map<String,String> param = (Map<String,String>)params.getParams();
        return sysConfigService.updateMsgTemp(param);
    }

    @RequestMapping(value = "/ext/delete",method = RequestMethod.GET)
    public Object deleteExt(@RequestParam Long extID){
        return sysConfigService.deleteExt(extID);
    }

    @ApiOperation(value="开账分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="pageIndex",dataType ="int",required=true,value="从第几条开始",defaultValue="1"),
            @ApiImplicitParam(paramType="query",name="pageSize",dataType ="int",required=true,value="每页多少条",defaultValue="5"),
            @ApiImplicitParam(paramType="query",name="sortField",dataType ="String",required=false,value="排序字段",defaultValue=""),
            @ApiImplicitParam(paramType="query",name="sortOrder",dataType ="String",required=false,value="排序方式（asc，desc）",defaultValue="asc")
    })
    @RequestMapping(value = "/selectAll",method = RequestMethod.POST)
    public Map get(HttpServletRequest request){
        Map<String, String> params = CommonUtils.getParameterMap(request);
        return sysConfigService.selectAllCompany(params);
    }

   //查询全部公司信息，不分页
    @RequestMapping(value = "/selectAll_notPage",method = RequestMethod.GET)
    public Object selectAll_notPage(HttpServletRequest request){
        return sysConfigService.selectAllCompany_notPage();
    }


    @ApiOperation(value="查询公司扩展信息Map")
    @RequestMapping(value = "/selectAllExtByCID",method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="companyInfoID",dataType ="long",required=true,value="公司主键")})
    public Object selectAllExtByCID(@RequestParam Long companyInfoID,HttpServletRequest request){
        return sysConfigService.selectAllExtByCID(companyInfoID);
    }

    @ApiOperation(value="查询公司app配置信息")
    @RequestMapping(value = "/selectAllExtForApp",method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="companyInfoID",dataType ="long",required=true,value="公司主键")})
    public Object selectAllExtForApp(@RequestParam Long companyInfoID,HttpServletRequest request){
        return sysConfigService.selectAllExtForApp(companyInfoID);
    }

    @ApiOperation(value="查询公司app配置信息")
    @RequestMapping(value = "/selectAllExtForFtp",method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="companyInfoID",dataType ="long",required=true,value="公司主键")})
    public Object selectAllExtForFtp(@RequestParam Long companyInfoID,HttpServletRequest request){
        return sysConfigService.selectAllExtForApp(companyInfoID);
    }
 /*   @ApiOperation(value="查询默认短信模板")
    @RequestMapping(value = "/selectTempByCID/isDefault",method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="companyInfoID",dataType ="long",required=true,value="公司主键")})
    public ResultData getTempIsDef(@RequestParam Long companyInfoID,HttpServletRequest request){
        return sysConfigService.selectTempIsDef(companyInfoID);
    }*/

    @ApiOperation(value="根据公司ID和模板code查询短信模板")
    @RequestMapping(value = "/selectTempByType",method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="companyInfoID",dataType ="long",required=true,value="公司主键"),
            @ApiImplicitParam(paramType="query",name="type",dataType ="int",required=true,value="短信类型(1验证码2下单通知3发货通知4账号审核5erp对接异常通知)")
    })
    public Object selectTempByCode(@RequestParam Long companyInfoID,@RequestParam int type,HttpServletRequest request){
        return sysConfigService.selectTempByCode(companyInfoID,type);
    }

    @ApiOperation(value="查询公司扩展信息根据code获取val")
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="companyID",dataType ="long",required=true,value="公司主键",defaultValue=""),
            @ApiImplicitParam(paramType="query",name="code",dataType ="string",required=true,value="code",defaultValue="")})
    @RequestMapping(value = "/getValToObj", method = RequestMethod.GET)
    public Object getValToObj(@RequestParam Long companyID,@RequestParam String code) {
        ResultData resultData = new ResultData();
        resultData.setData(sysConfigService.get(companyID,code));
        return resultData;
    }

    @ApiOperation(value="查询公司扩展信息根据code获取val")
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="companyID",dataType ="long",required=true,value="公司主键",defaultValue=""),
            @ApiImplicitParam(paramType="query",name="code",dataType ="string",required=true,value="code",defaultValue="")})
    @RequestMapping(value = "/getValToStr", method = RequestMethod.GET)
    public Object getValToStr(@RequestParam Long companyID,@RequestParam String code) {
        ResultData resultData = new ResultData();
        resultData.setData(sysConfigService.getString(companyID,code));
        return resultData;
    }

    @ApiOperation(value="查询公司扩展信息根据code获取val")
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="companyID",dataType ="long",required=true,value="公司主键",defaultValue=""),
            @ApiImplicitParam(paramType="query",name="code",dataType ="string",required=true,value="code",defaultValue="")})
    @RequestMapping(value = "/getValToInt", method = RequestMethod.GET)
    public Object getValToInt(@RequestParam Long companyID,@RequestParam String code) {
        ResultData resultData = new ResultData();
        resultData.setData(sysConfigService.getInt(companyID,code));
        return resultData;
    }

    @ApiOperation(value="查询公司扩展信息根据code获取val")
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="companyID",dataType ="long",required=true,value="公司主键",defaultValue=""),
            @ApiImplicitParam(paramType="query",name="code",dataType ="string",required=true,value="code",defaultValue="")})
    @RequestMapping(value = "/getValToBool", method = RequestMethod.GET)
    public Object getBoolean(@RequestParam Long companyID,@RequestParam String code) {
        ResultData resultData = new ResultData();
        resultData.setData(sysConfigService.getBoolean(companyID,code));
        return resultData;
    }

    @ApiOperation(value="查询公司扩展信息根据code获取val")
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="companyID",dataType ="long",required=true,value="公司主键",defaultValue=""),
            @ApiImplicitParam(paramType="query",name="code",dataType ="string",required=true,value="code",defaultValue="")})
    @RequestMapping(value = "/getValToLong", method = RequestMethod.GET)
    public Object getLong(@RequestParam Long companyID,@RequestParam String code) {
        ResultData resultData = new ResultData();
        resultData.setData(sysConfigService.getLong(companyID,code));
        return resultData;
    }

    @ApiOperation(value="查询公司扩展信息根据code获取val")
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="companyID",dataType ="long",required=true,value="公司主键",defaultValue=""),
            @ApiImplicitParam(paramType="query",name="code",dataType ="string",required=true,value="code",defaultValue="")})
    @RequestMapping(value = "/getValToJSON", method = RequestMethod.GET)
    public Object getJSONArray(@RequestParam Long companyID,@RequestParam String code) {
        ResultData resultData = new ResultData();
        resultData.setData(sysConfigService.getJSONArray(companyID,code));
        return resultData;
    }

    @ApiOperation(value="获取支付方式")
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="cID",dataType ="long",required=true,value="公司主键",defaultValue="")})
    @RequestMapping(value = "/payment/id", method = RequestMethod.GET)
    public Object getPayment(@RequestParam Long cID) {
        return sysPaymentService.getPaymentInfo(cID);
    }

    @ApiOperation(value ="保存支付方式")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="form",name="code",dataType ="String",required=true,value="key字母下划线数字",defaultValue=""),
            @ApiImplicitParam(paramType="form",name="value",dataType ="long",required=true,value="key对应得值",defaultValue=""),
            @ApiImplicitParam(paramType="form",name="remark",dataType ="String",required=false,value="备注",defaultValue=""),
    })
    @RequestMapping(value = "/payment/update",method = RequestMethod.POST)
    public Object updatePayment(@RequestBody SysPayment sysPayment){
        return sysPaymentService.update(sysPayment);
    }

    @ApiOperation(value="删除所有订单")
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="companyID",dataType ="Long",required=true,value="companyID",defaultValue="")})
    @RequestMapping(value = "/delOrderByCompanyID", method = RequestMethod.GET)
    public Object delOrderByCompanyID(@RequestParam Long companyID) {
        return employeeService.delOrderByCompanyID(companyID);
    }

    @ApiOperation(value="删除所有商品")
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="companyID",dataType ="Long",required=true,value="companyID",defaultValue="")})
    @RequestMapping(value = "/delGoodsByCompanyID", method = RequestMethod.GET)
    public Object delGoodsByCompanyID(@RequestParam Long companyID) {
        return employeeService.deleteGoodsByCompanyID(companyID);
    }

    @ApiOperation(value="删除所有用户（不包括超管和总行管理员）")
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="companyID",dataType ="Long",required=true,value="companyID",defaultValue="")})
    @RequestMapping(value = "/delAllUserInfo", method = RequestMethod.GET)
    public Object delAllUserInfo(@RequestParam Long companyID) {
        return employeeService.delAllUserInfo(companyID);
    }

    @ApiOperation(value="删除组织机构（不包括开账初始化的总行）")
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="companyID",dataType ="Long",required=true,value="companyID",defaultValue="")})
    @RequestMapping(value = "/delOrg", method = RequestMethod.GET)
    public Object delOrg(@RequestParam Long companyID) {
        return employeeService.delOrg(companyID);
    }

    @ApiOperation(value="查询APP版本记录")
    @ApiImplicitParams({@ApiImplicitParam(paramType="query",name="cID",dataType ="long",required=true,value="公司主键",defaultValue="796235203885682688")})
    @RequestMapping(value = "/appInfo", method = RequestMethod.GET)
    public Object getAppInfo(@RequestParam Long cID) {
        return sysAppService.getAppInfo(cID);
    }


    @RequestMapping(value = "/updateAppInfo",method = RequestMethod.POST)
    public Object updateAppInfo(@RequestBody SysApp SysApp){
        return sysAppService.updateAppInfo(SysApp);
    }

    @RequestMapping(value = "/delAppInfo",method = RequestMethod.GET)
    public Object delAppInfo(@RequestParam Long appID){
        return sysAppService.deleteAppInfo(appID);
    }

    @RequestMapping(value = "/delAppFile",method = RequestMethod.GET)
    public ResultData delAppFile(@RequestParam Long appID){
        return sysAppService.deleteAppInfo(appID);
    }

    @RequestMapping(value = "/initAppExt/{template}/{newCompanyID}",method = RequestMethod.GET)
    public Object initAppExt(@PathVariable Long template,@PathVariable Long newCompanyID){
       return sysConfigService.initAppExt(template,newCompanyID);
    }
}
